package com.it.orderseata.service.impl;


import com.it.orderseata.api.StockService;
import com.it.orderseata.mapper.OrderMapper;
import com.it.orderseata.pojo.Order;
import com.it.orderseata.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class OrderServiceImpl implements OrderService {


    @Resource
    OrderMapper orderMapper;

    @Autowired
    StockService stockService;

    /**
     * 下单
     * @return
     */
    @GlobalTransactional    //开启全局事务 大事务
    public Order create(Order order) {
        // 插入能否成功？
        orderMapper.insert(order);

        // 扣减库存 能否成功？
        stockService.reduct(order.getProductId());

        // 异常
        int a=1/0;

        return order;
    }



    @Trace   //这里是skywalking的链路追踪
    @Tag(key="getAll",value="returnedObj")  //key是方法   value =   returnedObj  返回值
    public List<Order> all() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        return orderMapper.selectAll();
    }

    @Trace
    @Tags({@Tag(key="getAll",value="returnedObj"),
            @Tag(key="getAll",value="arg[0]")})   //   arg[0] 参数
    public Order get(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }
}
